package com.weiku.until;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.weiku.database.PsqlConnectionTool;
import com.weiku.database.SendSuccess;
import com.weiku.database.receipt;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class XML {
	public static SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
	
    public static Map<String,String> parseXML(String path) throws ParseException {
	    	Map<String,String> map=new HashMap<String,String>();
	        SAXReader reader = new SAXReader();
	        try {
	            Document document = reader.read(new File(path));
	            Element bookStore = document.getRootElement();
	            Iterator it = bookStore.elementIterator();
	            while (it.hasNext()) {
	                Element book = (Element) it.next();
	                List<Attribute> bookAttrs = book.attributes();
	                for (Attribute attr : bookAttrs) {
	                    System.out.println("属性名：" + attr.getName() + "--属性值："+ attr.getValue());
	                }
	                Iterator itt = book.elementIterator();
	                while (itt.hasNext()) {
	                    Element bookChild = (Element) itt.next();
	                  //System.out.println("节点名：" + bookChild.getName() + "--节点值：" + bookChild.getStringValue());
	                    map.put(bookChild.getName(),bookChild.getStringValue());
	                }
	            }
	        } catch (DocumentException e) {
	            e.printStackTrace();
	        }
	        return map; 
	    }

	public static Map<String, String> parseOrderReceiptXml(String srcPathStr) throws ParseException {
		Map<String,String> map=new HashMap<String,String>();
	    map =parseXML(srcPathStr);
		receipt receipt=new receipt();
        String content=FileUntil.ReadFile(srcPathStr);
        receipt.setContent(content.replaceAll("[\b\r\n\t]*", ""));
        receipt.setOrderId(map.get("orderNo"));
        receipt.setExpressNumber(map.get("orderNo"));
        receipt.setReceiptType("IEPT302_BACK");
        Date date=sf.parse(map.get("returnTime"));
        Timestamp ts =new Timestamp(date.getTime());
        receipt.setReceiptDate(ts);
        System.out.println(ts);
        PsqlConnectionTool.insertRecepit(receipt);
		return map;
	}

	public static Map<String, String> parseWaybillReceiptXml(String srcPathStr) throws ParseException {
		Map<String,String> map=new HashMap<String,String>();
		map=parseXML(srcPathStr);
		receipt receipt=new receipt();
        String content=FileUntil.ReadFile(srcPathStr);
        receipt.setContent(content.replaceAll("[\b\r\n\t]*", ""));
        receipt.setOrderId(map.get("logisticsNo"));
        receipt.setExpressNumber(map.get("logisticsNo"));
        receipt.setReceiptType("IEPT401");
        Date date=sf.parse(map.get("returnTime"));
        Timestamp ts =new Timestamp(date.getTime());
        receipt.setReceiptDate(ts);
        System.out.println(ts);
        PsqlConnectionTool.insertRecepit(receipt);
		return map;
	}

	public static Map<String, String> parsePaybillReceiptXml(String srcPathStr) throws ParseException {
		Map<String,String> map=new HashMap<String,String>();
		map =parseXML(srcPathStr);
		receipt receipt=new receipt();
        String content=FileUntil.ReadFile(srcPathStr);
        receipt.setContent(content.replaceAll("[\b\r\n\t]*", ""));
        receipt.setOrderId(map.get("orderNo"));
        receipt.setExpressNumber(map.get("payNo"));
        receipt.setReceiptType("IEPT201_BACK");
        Date date=sf.parse(map.get("returnTime"));
        Timestamp ts =new Timestamp(date.getTime());
        receipt.setReceiptDate(ts);
        System.out.println(ts);
        PsqlConnectionTool.insertRecepit(receipt);
		return map;
	}

	public static Map<String, String> parseListReceiptXml(String srcPathStr) throws ParseException, SQLException {
		Map<String,String> map=new HashMap<String,String>();
		map=parseXML(srcPathStr);
		receipt receipt=new receipt();
        String content=FileUntil.ReadFile(srcPathStr);
        receipt.setContent(content.replaceAll("[\b\r\n\t]*", ""));
        receipt.setOrderId(map.get("preNo"));
        receipt.setExpressNumber(map.get("invtNo"));
        receipt.setReceiptType("BGM001_BACK");
        Date date=sf.parse(map.get("returnTime"));
        Timestamp ts =new Timestamp(date.getTime());
        receipt.setReceiptDate(ts);
        System.out.println(ts);
        PsqlConnectionTool.insertRecepit(receipt);
        if(map.get("returnStatus").equals("800")){
        	double price=PsqlConnectionTool.selectOrdersumByOrderId(map.get("orderNo"));
        	double sum=PsqlConnectionTool.selectSendSuccessByOrderId(map.get("orderNo"));
        	SendSuccess sendSuccess=new SendSuccess();
        	sendSuccess.setOrderSum(new BigDecimal(price));
        	sendSuccess.setSum(new BigDecimal(price+sum));
        	sendSuccess.setOrderId(map.get("orderNo"));
        	sendSuccess.setXml(content.replaceAll("[\b\r\n\t]*", ""));
        	sendSuccess.setSendTime(date);
        	sendSuccess.setAddTime(new Date());
        	PsqlConnectionTool.insertSendSuccess(sendSuccess);
        }
		return map;
	}

	
}